using System;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
using Enum;
using Manager;
using Struct;

namespace Data {

    public class FightProperties {

        /// <summary>
        /// 复苏之魂 3004 可用的复活次数
        /// </summary>
        public int reviveCount = 1;

        /// <summary>
        /// 选择技能面板显示的技能数量
        /// </summary>
        public int selectSkillPanelCount = 3;

        /// <summary>
        /// 选择技能面板最大刷新次数
        /// </summary>
        public int selectSkillPanelMaxRefreshCount = 0;

        /// <summary>
        /// 局内属性 部分技能增加的属性
        /// </summary>
        private Dictionary<EnumPropertiesType, PropertiesStruct> properties = new();


        /// <summary>
        /// 技能字典 技能id,技能等级
        /// </summary>
        private Dictionary<int, int> skillDict = new();

        public bool isPlay;

        public BaseCharacterController characterController;

        public FightProperties(Dictionary<EnumPropertiesType, PropertiesStruct> properties, bool isPlay, BaseCharacterController characterController) {
            this.properties = properties;
            this.isPlay = isPlay;
            this.characterController = characterController;
        }

        public FightProperties(bool isPlay, BaseCharacterController characterController) {
            this.isPlay = isPlay;
            this.characterController = characterController;
        }


        public int GetNextUpgradeExp() {
            return 100;
            // return GetProperties(EnumPropertiesType.Level).value * 100;
        }

        public PropertiesStruct GetProperties(EnumPropertiesType propertyType) {
            PropertiesStruct property;
            if (properties.ContainsKey(propertyType)) {
                property = properties[propertyType];
                return property;
            } else {
                property = new PropertiesStruct() { propertiesType = propertyType, value = 0 };
                properties[propertyType] = property;
                return property;
            }
        }

        public void AddProperty(EnumPropertiesType propertyType, PropertiesStruct propertyStruct, bool sendAnimationEvent = true) {
            PropertiesStruct propertiesStruct = GetProperties(propertyType);
            propertiesStruct.ComputedProperties(propertyStruct);
            SetProperties(propertiesStruct, sendAnimationEvent: sendAnimationEvent);
        }

        public void AddProperty(EnumPropertiesType propertiesType, int value, bool isPercentage = false, bool sendAnimationEvent = true) {
            PropertiesStruct propertiesStruct = GetProperties(propertiesType);
            (int oldValue, int newValue) = propertiesStruct.ComputedProperties(new PropertiesStruct() { propertiesType = propertiesType, isPercentage = isPercentage, value = value });
            propertiesStruct.tempValue = newValue - oldValue;
            SetProperties(propertiesStruct, sendAnimationEvent: sendAnimationEvent);


        }

        public void SetProperties(EnumPropertiesType propertyType, int value, bool isPercentage = false, bool sendAnimationEvent = true) {
            SetProperties(new PropertiesStruct() { propertiesType = propertyType, isPercentage = isPercentage, value = value });
        }

        /// <summary>
        ///     设置属性值
        /// </summary>
        /// <param name="propertyType"> 属性枚举</param>
        /// <param name="propertiesStruct">属性值</param>
        /// <param name="sendEvent">是否发送通知</param>
        /// <param name="sendAnimationEvent"> 是否更新动画</param>
        public void SetProperties(PropertiesStruct propertiesStruct, bool sendEvent = true, bool sendAnimationEvent = true) {
            PropertiesStruct p = GetProperties(propertiesStruct.propertiesType);
            if (propertiesStruct.propertiesType == EnumPropertiesType.HP) {
                PropertiesStruct maxHp = GetProperties(EnumPropertiesType.MaxHp);
                if (p.value >= maxHp.value && maxHp.value > 0) {
                    propertiesStruct.value = maxHp.value;
                }
            }

            //如果属性小于1 则改为1
            switch (propertiesStruct.propertiesType) {
                case EnumPropertiesType.Attack:
                case EnumPropertiesType.Defense:
                case EnumPropertiesType.MaxHp:
                case EnumPropertiesType.UpgradeExp:
                    propertiesStruct.value = Math.Max(propertiesStruct.value, 1);
                    break;
            }


            properties[p.propertiesType] = propertiesStruct;
            if (sendEvent && isPlay) {
                EnumEventSystem.Global.Send(EnumGameEvent.OnChangeFightPlayerProperties, propertiesStruct, sendAnimationEvent);
            }

            if (propertiesStruct.propertiesType == EnumPropertiesType.MaxHp) {
                //增加最大生命值的时候,也要增加当前生命值
                AddProperty(EnumPropertiesType.HP, propertiesStruct.tempValue, sendAnimationEvent: false);
            }
            if (propertiesStruct.propertiesType == EnumPropertiesType.Exp) {
                CheckUpgradeLevel();
            }
        }


        private void CheckUpgradeLevel() {
            //获取当前经验值
            PropertiesStruct exp = GetProperties(EnumPropertiesType.Exp);
            PropertiesStruct upgradExp = GetProperties(EnumPropertiesType.UpgradeExp);
            while (exp.value >= upgradExp.value) {
                AddProperty(EnumPropertiesType.Level, 1);
                LevelUpTrigger();
                exp.value -= upgradExp.value;

                upgradExp.value = GetNextUpgradeExp();
                SetProperties(upgradExp);
                SetProperties(exp);
            }
        }

        /// <summary>
        /// 角色升级触发
        /// </summary>
        private void LevelUpTrigger() {
            UIRoot.Instance.SetEventSystem(false);
            GameLayer.Instance.GameLayerData.AddOrdinaryMessageToScene($"升级,快选一个技能吧!");
            GameLayer.Instance.PlayerController.PlayLevelUpAnimation((e) => {
                UIRoot.Instance.SetEventSystem(true);
                UIKit.OpenPanel<SelectSkillPanel>(UILevel.PopUI);
            });


        }


        public void AddFightRewards(List<PropertiesStruct> structRewards) {
            foreach (var item in structRewards) {
                AddProperty(item.propertiesType, item.value, item.isPercentage);
            }
        }


    }

}